package com.example.zhou.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.example.zhou.androidmusic.R;
import com.example.zhou.filehelper.AlbumUtil;
import com.example.zhou.musicmodel.MusicBean;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 11065 on 2016/11/15.
 */

public class MusicInfoAdapter extends RecyclerView.Adapter<MusicInfoAdapter.MusicViewHolder> {
    private Context context;
    private List<MusicBean> musicBeen = new ArrayList<>();
    private LayoutInflater inflater;
    public BitmapRequestBuilder<GlideUrl, Bitmap> requestBuilder;

    public MusicInfoAdapter(Context context, List<MusicBean> musicBeen) {
        this.context = context;
        this.musicBeen = musicBeen;
        this.inflater = LayoutInflater.from(context);

        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .animate(android.R.anim.fade_in)
                .centerCrop()
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    @Override
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.music_list_item, parent, false);
        MusicViewHolder musicViewHolder = new MusicViewHolder(view);
        return musicViewHolder;
    }

    @Override
    public void onBindViewHolder(MusicViewHolder holder, int position) {
        byte[] bytes = AlbumUtil.getMusicAlbum(new File(musicBeen.get(position).getFilepath()));

        Glide.with(context).load(bytes).into(holder.iv_music_image);
        MusicBean musicBean = this.musicBeen.get(position);
        holder.tv_music_name.setText(musicBean.getName());
        String album = musicBean.getAlbum().equals("unknow") ? "未知专辑" : musicBean.getAlbum();
        holder.tv_album.setText(album);
        String songer = Objects.equals(musicBean.getArtist(), "unknow") ? "未知歌手" : musicBean.getArtist();
        holder.tv_songer.setText(songer);
    }

    @Override
    public int getItemCount() {
        return musicBeen.size();
    }

    public class MusicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_album)
        TextView tv_album;
        @BindView(R.id.tv_songer)
        TextView tv_songer;
        @BindView(R.id.tv_music_name)
        TextView tv_music_name;
        @BindView(R.id.iv_music_image)
        ImageView iv_music_image;

        public MusicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    interface OnClickListener {
        void onClick(View view, MusicBean musicBean, int position);
    }
}
